import { drawerConfig, drawerTitle, drawerVisible } from "./drawer";
import { modalConfig, modalTitle, modalVisible } from "./modal";
import { searchConfig } from "./search";
import { tableData, tableConfig, tablePage, tableLoading } from "./table";
import {
  defaultSearchList,
  dicts,
  userFormState,
  isNotShowAuthTime,
  isNotShowCheckedTime,
  isShowAuthInfo,
  isShowAuthProcess,
  isShowAuthVideo,
  isShowIdentityInfo,
  isShowRealAuthPhoto,
  isShowSpecialPopularityLevelBtn,
  isShowSubmitBtn,
  modalFormState,
  operateType,
  queryParams,
  searchList,
  tabPower,
  tableActiveKey,
  tipsObject,
  drawerFormState,
  resetFormState,
} from "./store";
import { fetchPopularDataById, fetchDicts, fetchInfo } from "./utils";
import { isHasPower } from "@/assets/directives/tools";
export const refreshTable = async (searchParams: any = {}) => {
  tablePage.current = 1;
  initObjAttr(queryParams, searchParams);
  await fetchList();
};
const defineFetchApiMap = () => {
  return new Map([
    [0, () => popularApi.query(queryParams)],
    [1, () => popularApi.queryAuditList(queryParams)],
  ]);
};
const handleFetchApiTask = async (number) => {
  try {
    const [, api]: any = [...defineFetchApiMap()].find(
      ([key]) => key === number
    );
    tableData.value.length = 0;
    tableLoading.value = true;
    const result: any = await api();
    if (result.code != 200) return message.error(result.message);
    tableData.value = result?.data?.list ?? [];
    tablePage.total = result?.data?.attributes?.total ?? 0;
  } catch (error: any) {
    message.error(error.message);
  } finally {
    tableLoading.value = false;
  }
};
export const fetchList = async () => {
  queryParams.page = tablePage.current;
  queryParams.page_size = tablePage.pageSize;
  await handleFetchApiTask(tableActiveKey.value);
};
const tmpObject = ref<any>({});
const showDrawer = () => (drawerVisible.value = true);
const showModal = () => {
  if (operateType.value === "updateLevel")
    modalFormState.editId = tmpObject.value.id;
  if (operateType.value === "agree" || operateType.value === "refuse")
    modalFormState.id = tmpObject.value.id;
  modalVisible.value = true;
};
const defineApiMap = (number, string, record) => {
  const updateLevel = () => {
    modalTitle.value = $t("base.specifyPopularityLevel");
    showModal();
  };
  const searchDetail = async () => {
    drawerTitle.value = $t("base.popularDetail");
    resetObjAttr(userFormState);
    const res = await fetchDicts();
    dicts.value = res;
    await fetchPopularDataById({
      infoId: record.id,
    });
    if (number === 0) {
      isNotShowAuthTime.value = record.status != 1;
      isShowSpecialPopularityLevelBtn.value = record.status === 0;
      isShowAuthProcess.value = true;
    }
    if (number === 1) {
      isNotShowCheckedTime.value = record.status === 4;
      isShowSubmitBtn.value = record.status === 4;
      isShowSpecialPopularityLevelBtn.value = false;
      isShowAuthProcess.value = false;
    }
    isShowRealAuthPhoto.value = false;
    isShowAuthVideo.value = true;
    isShowAuthInfo.value = true;
    isShowIdentityInfo.value = true;
    showDrawer();
  };
  const handleAgree = () => {
    modalTitle.value = $t("base.agreeAuth");
    modalFormState.status = 0;
    showModal();
  };
  const handleRefuse = () => {
    modalTitle.value = $t("base.refuseAuth");
    modalFormState.status = 6;
    showModal();
  };
  return new Map([
    [{ key: "updateLevel", state: /[0-1]/ }, updateLevel],
    [{ key: "view", state: /[0-1]/ }, searchDetail],
    [{ key: "agree", state: /[1]/ }, handleAgree],
    [{ key: "refuse", state: /[1]/ }, handleRefuse],
  ]);
};

const handleActionTask = (number, string, record) => {
  const [, api]: any = [...defineApiMap(number, string, record)].find(
    ([object]) => object.key === string && object.state.test(number)
  );
  api();
};

export const handleAction = async ({ record, type }: any) => {
  operateType.value = type;
  if (!isEmptyType(record)) tmpObject.value = record;
  resetFormState(drawerFormState);
  handleActionTask(tableActiveKey.value, type, record);
};
export default defineComponent({
  setup() {
    tabPower[0] = isHasPower("popularApi.query");
    tabPower[1] = isHasPower("popularApi.queryAuditList");
    tableActiveKey.value = tabPower[0] ? 0 : tabPower[1] ? 1 : Infinity;
    searchList.value = defaultSearchList[tableActiveKey.value] ?? [];
    const defineSearch = searchConfig();
    const defineDrawer = drawerConfig();
    const defineModal = modalConfig();
    const defineTable = tableConfig();
    watch(
      () => sysZone.value,
      () => fetchList()
    );
    onActivated(() => {
      tipsObject.title = $t("popular9");
      tipsObject.content = $t("popular10");
      fetchList();
      fetchInfo();
    });
    return () => (
      <>
        <s-design
          defineSearch={defineSearch}
          defineTable={defineTable}
          defineDrawer={defineDrawer}
          defineModal={defineModal}
        />
      </>
    );
  },
});
